using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using VSSDKAssist.Assets.CustomPages;
using Microsoft.Practices.WizardFramework;
using VSSDKAssist.Assets.Converters;
using System.Globalization;
using System.Collections.Specialized;
using VSSDKAssist.Assets.Helpers;
using EnvDTE;
using VSSDKAssist.Recipes.Common.Helpers;

namespace VSSDKAssist.Recipes.CreateVSService
{
	/// <summary>
	/// Custom page for CreateVSService recipe
	/// </summary>
	public partial class CreateVSServiceCP : WizardPageBase
	{
		private ListViewItem sService;
		private ListViewItem iService;
		private ListViewItem service;

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSServiceCP"/> class.
		/// </summary>
		public CreateVSServiceCP()
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="CreateVSServiceCP"/> class.
		/// </summary>
		/// <param name="wizard">The <see cref="T:Microsoft.Practices.WizardFramework.WizardForm"/>that will host the new page.</param>
		public CreateVSServiceCP(WizardForm wizard)
			: base(wizard)
		{
			InitializeComponent();
		}

		/// <summary>
		/// Gets or sets the name of the service.
		/// </summary>
		/// <value>The name of the service.</value>
		[RecipeArgument]
		public string ServiceName
		{
			get { return veServiceName.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veServiceName.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the destination folder.
		/// </summary>
		/// <value>The destination folder.</value>
		[RecipeArgument]
		public string DestinationFolder
		{
			get { return veDestinationFolder.Value.ToString(); }
			set
			{
				if(value != null)
				{
					veDestinationFolder.TextBox.Text = value;
				}
			}
		}

		/// <summary>
		/// Gets or sets the site of the control.
		/// </summary>
		/// <value></value>
		/// <returns>The <see cref="T:System.ComponentModel.ISite"></see> associated with the <see cref="T:System.Windows.Forms.Control"></see>, if any.</returns>
		/// <PermissionSet><IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/><IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence"/><IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true"/></PermissionSet>
		public override ISite Site
		{
			get
			{
				return base.Site;
			}
			set
			{
				base.Site = value;
				if(value != null)
				{
					this.veServiceName.BeginInit();
					this.veServiceName.ValueType = typeof(string);
					base.Site.Container.Add(veServiceName);
					this.veServiceName.ConverterInstance = new CSharpIdentifierConverter();
					this.veServiceName.EndInit();

					this.veDestinationFolder.BeginInit();
					this.veDestinationFolder.ValueType = typeof(string);
					base.Site.Container.Add(veDestinationFolder);
					this.veDestinationFolder.ConverterInstance = new CSharpIdentifierConverter();
					this.veDestinationFolder.EndInit();
				}
			}
		}

		private void CreateVSServiceCP_Load(object sender, EventArgs e)
		{
			CreateListViewItems();
		}

		private void CreateListViewItems()
		{
			sService = new ListViewItem(lstServices.Groups["SService"]);
			iService = new ListViewItem(lstServices.Groups["IService"]);
			service = new ListViewItem(lstServices.Groups["ServiceImp"]);

			lstServices.Items.AddRange(new ListViewItem[] {
		        sService, iService, service
		    });
		}

		private void veServiceName_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veServiceName.ConverterInstance != null)
			{
				errorProvider.SetError(veServiceName, string.Empty);

				if(veServiceName.Value != null)
				{
					if(CustomPageHelper.ProjectItemExists(
						DictionaryService,
						"VSPackageProject",
						"DestinationFolder",
						string.Concat(veServiceName.Value.ToString(), Properties.Resources.CsExtension)))
					{
						DictionaryService.SetValue("ServiceName", null);
						errorProvider.SetError(veServiceName, Properties.Resources.InvalidValue);
					}
					else
					{
						DictionaryService.SetValue("ServiceName", veServiceName.Value.ToString());
					}
				}
				else
				{
					DictionaryService.SetValue("ServiceName", null);
					errorProvider.SetError(veServiceName, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("ServiceName", veServiceName.Value.ToString());
			}

			UpdatePreview();
		}

		private void veDestinationFolder_ValueChanged(object sender, System.ComponentModel.Design.ComponentChangedEventArgs e)
		{
			if(veDestinationFolder.ConverterInstance != null)
			{
				errorProvider.SetError(veDestinationFolder, string.Empty);

				if(veDestinationFolder.Value != null)
				{
					DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
				}
				else
				{
					DictionaryService.SetValue("DestinationFolder", null);
					errorProvider.SetError(veDestinationFolder, Properties.Resources.InvalidValue);
				}
			}
			else
			{
				DictionaryService.SetValue("DestinationFolder", veDestinationFolder.Value.ToString());
			}

			UpdatePreview();
		}

		private void UpdatePreview()
		{
			string destinationFolder = DictionaryService.GetValue("DestinationFolder") as string;
			string serviceName = DictionaryService.GetValue("ServiceName") as string;

			if(destinationFolder != null && serviceName != null)
			{
				sService.Text = string.Format(CultureInfo.CurrentCulture, @"{0}\S{1}.cs", destinationFolder, serviceName);
				iService.Text = string.Format(CultureInfo.CurrentCulture, @"{0}\I{1}.cs", destinationFolder, serviceName);
				service.Text = string.Format(CultureInfo.CurrentCulture, @"{0}\{1}.cs", destinationFolder, serviceName);
			}
			else
			{
				sService.Text = string.Empty;
				iService.Text = string.Empty;
				service.Text = string.Empty;
			}
		}
	}
}